package com.bosssoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bosssoft.entity.dto.FibVoucherDTO;
import com.bosssoft.entity.po.FibVoucherPO;
import com.bosssoft.entity.vo.FibVoucherVO;
import com.bosssoft.enums.EleSerCode;
import com.bosssoft.exception.DaoException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bosssoft.mapper.FibVoucherMapper;
import com.bosssoft.service.FibVoucherService;
import org.springframework.util.StringUtils;


/**
 * @author Li kexi
 * Created on 2021/8/20.10:12
 */
@Slf4j
@Service
public class FibVoucherServiceImpl extends ServiceImpl<FibVoucherMapper, FibVoucherPO> implements FibVoucherService {

    @Override
    public void updateDeleted(FibVoucherDTO fibVoucherDTO) {
        FibVoucherPO fibVoucherPO = new FibVoucherPO();
        BeanUtils.copyProperties(fibVoucherDTO, fibVoucherPO);
        //id判空
        if (StringUtils.isEmpty(fibVoucherPO.getVoucherCode())) {
            log.info("更新id为空,in voucher_updateDeleted方法");
            throw DaoException.error(EleSerCode.VOUCHER_CODE_ID_NOT_FOUND);
        }
        //创建人判空
        if (StringUtils.isEmpty(fibVoucherPO.getUpdateBy())) {
            throw DaoException.error(EleSerCode.UPDATEBY_NAME_NOT_FOUND);
        }
        QueryWrapper<FibVoucherPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("voucher_code", fibVoucherPO.getVoucherCode());
        FibVoucherPO sqlPO = baseMapper.selectOne(queryWrapper);
        //数据库记录判空
        if (sqlPO == null) {
            log.info("前台传id值为"+fibVoucherPO.getVoucherCode()+"根据code查找,数据库没有记录,in voucher_updateDeleted方法");
            throw DaoException.error(EleSerCode.VOUCHER_RECODE_NOT_FOUND);
        }
        //该条记录已禁用
        if (StringUtils.isEmpty(sqlPO.getDeleted())) {
            log.info("id值为"+fibVoucherPO.getVoucherCode()+"的记录已被禁用,in voucher_updateDeleted方法");
            throw DaoException.error(EleSerCode.VOUCHER__RECODE_IS_DELETED);
        }
        fibVoucherPO.setDeleted((byte) -1);
        baseMapper.updateById(fibVoucherPO);
    }

    @Override
    public int update(FibVoucherDTO record) {
        FibVoucherPO fibVoucherPO = new FibVoucherPO();
        BeanUtils.copyProperties(record, fibVoucherPO);
        //空编码执行新增,但凭证模板不能新增
        if(StringUtils.isEmpty(fibVoucherPO.getVoucherCode())){
            log.error("用户:"+fibVoucherPO.getCreateBy()+"创建失败");
            throw DaoException.error(EleSerCode.VOUCHER_CANNOT_BE_ADD);
        }
        return baseMapper.insertOrUpdate(fibVoucherPO);
    }

    @Override
    public int insertOrUpdateSelective(FibVoucherDTO record) {
        return 0;
    }


    @Override
    public int updateBatch(List<FibVoucherDTO> list) {
        return 0;
    }

    @Override
    public int updateBatchSelective(List<FibVoucherDTO> list) {
        return 0;
    }

    @Override
    public int batchInsert(List<FibVoucherDTO> list) {
        return 0;
    }
    //@Override
    //public int updateBatch(List<FibVoucherDTO> list) {
    //    FibVoucherPO fibVoucherPO = new FibVoucherPO();
    //    return baseMapper.updateBatch(list);
    //}
    //@Override
    //public int updateBatchSelective(List<FibVoucherDTO> list) {
    //    return baseMapper.updateBatchSelective(list);
    //}
    //@Override
    //public int batchInsert(List<FibVoucherDTO> list) {
    //    return baseMapper.batchInsert(list);
    //}

    //@Override
    //public int insertOrUpdateSelective(FibVoucherDTO record) {
    //    return 0;
    //}
    //@Override
    //public int insertOrUpdateSelective(FibVoucherDTO record) {
    //    return baseMapper.insertOrUpdateSelective(record);
    //}
}
